package gxprod.androidai.widgets;

import gxprod.androidai.AIActivity;
import gxprod.androidai.R;
import gxprod.androidai.functions.OrganizationFunction.Event;
import java.util.Vector;

import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Manages Bubbles. This will need changes to allow widgets to be displayed.
 * @author Josh Maleszewski
 */
public class EventAdapter extends BaseAdapter implements OnItemClickListener {

	private Vector<Event> items;
	private LayoutInflater mInflater; 
	public AIActivity activity;

	private StringBuilder sb = new StringBuilder();

	public boolean isMute = false;

	public EventAdapter(AIActivity context) {
		items = new Vector<Event>();
		mInflater = LayoutInflater.from(context);
		activity = context;
	}

	public int getCount() { return items.size();}
	public Event getItem(int position) { return items.get(position); }
	public long getItemId(int position) { return position; }
	public Vector<Event> getArray() { return items; }

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		Event item = items.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.eventitem, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.textView1);
			holder.subTitle = (TextView) convertView.findViewById(R.id.textView2);
			holder.dates = (TextView) convertView.findViewById(R.id.textView3);
			convertView.setTag(holder);
		} else 
			holder = (ViewHolder) convertView.getTag();

		holder.title.setText(item.Name);
		holder.subTitle.setText(item.Details);

		
		if(item.AllDay) {
			sb.append(DateFormat.getLongDateFormat(activity).format(item.Begin));
		} else {
			boolean sameDay = (item.Begin.getDate() == item.End.getDate()) & (item.Begin.getYear() == item.End.getYear());
			if(sameDay) {
				sb.append(DateFormat.getMediumDateFormat(activity).format(item.Begin));
				sb.append("   ");
				sb.append(DateFormat.getTimeFormat(activity).format(item.Begin));
				sb.append(" - ");
				sb.append(DateFormat.getTimeFormat(activity).format(item.End));
			}
		}
		holder.dates.setText(sb.toString());
		sb.delete(0, sb.length());
		
		return convertView;
	}

	public void addItem(Event event) {
		items.add(event);
	}

	public void postUpdate() {
		activity.runOnUiThread(new Runnable() {
			public void run() {
				notifyDataSetChanged();
			}
		});
	}
	public void postAddItem(final Event event) {
		if(isMute) 
			return;
		activity.runOnUiThread(new Runnable() {
			public void run() {
				addItem(event);
			}
		});
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		this.activity.list.setSelectionFromTop(getCount() - 1, activity.list.getHeight() / 5 * 4);
	}


	public static class ViewHolder {
		public TextView title;
		public TextView dates;
		public TextView subTitle;
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}
}